package my.merborn.ordersysteam105.models.servicesImpl;

import my.merborn.ordersysteam105.models.dao.MenuMapper;
import my.merborn.ordersysteam105.models.dao.jpa.MenuMapperJPA;
import my.merborn.ordersysteam105.models.pojo.Menu;
import my.merborn.ordersysteam105.models.services.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuMapper mm;

    @Autowired
    MenuMapperJPA mmj;

    @Override
    public List<Menu> getALL() {
        List<Menu> menus = mm.selectALL();
        return menus;
    }

    @Override
    public List<Menu> getALLByPageable(Pageable pageable) {
        Page<Menu> all = mmj.findAll(pageable);
        List<Menu> content = all.getContent();
        return content;
    }

    @Override
    public boolean addNewFood(Menu food) {
        if(food.getmId()!=null){
            return false;
        }else if(food.getmPrice()==null||food.getmName()==null||food.getmCount()!=0){
            return false;
        }
        if(mm.insertSelective(food)==0)return false;
        else
        return true;
    }

    @Override
    public boolean updateFood(Menu updatedFood) {
        if(updatedFood.getmId()==null){
            return false;
        }
        if(mm.updateByPrimaryKeySelective(updatedFood)==0)return false;
        return true;
    }

    @Override
    public boolean deleteFood(Menu food) {
        if(food.getmId()==null)return false;
         if(mm.deleteByPrimaryKey(food.getmId())==0)return false;
         else return true;
    }

    @Override
    public List<Menu> MenuRankingListOrderByDESC() {
        List<Menu> menus = mm.selectALLMenuByMcountDESC();
        return menus;
    }

    @Override
    public Menu incrementMenuCount(Menu menu) {
        Integer oldMenuCount = menu.getmCount();
        System.out.println("menu's name "+menu.getmName()+" old m_count is "+ oldMenuCount);
        menu.setmCount(++oldMenuCount);
        mm.updateByPrimaryKeySelective(menu);
        return menu;
    }

    @Override
    public void incrementMenuCountByOrderList(String data) {
        String[] split = data.split("@");
        for (String s : split) {
            Menu select = mm.selectByPrimaryKey(Integer.parseInt(s));
            incrementMenuCount(select);
        }
    }

}
